<?php
if (!defined('SITE_PATH')) exit();
header('Content-Type: text/html; charset=utf-8');

$sql_file  = APPS_PATH.'/cms/Appinfo/install.sql';
//执行sql文件
$res = M('')->executeSqlFile($sql_file);
if(!empty($res)){//错误
	echo $res['error_code'];
	echo '<br />';
	echo $res['error_sql'];
	//清除已导入的数据
	include_once(APPS_PATH.'/cms/Appinfo/uninstall.php');
	exit;
} else {
	$data['plistid'] = '0';
	$data['cname'] = '首页';
	$data['catetemplate'] = 'channel.html';
	$data['arttemplate'] = 'content.html';
	$data['createurl'] = '';
	$data['catenaming'] = 'channels-{cateid}.html';
	$data['artnaming'] = 'contents-{contentid}.html';
	$mid = $this->mid;
	$data['create_uid'] = $mid;
	$data['create_uname'] = common('String')->getUserName($mid);
	$data['update_uid'] = $mid;
	$data['update_uname'] = common('String')->getUserName($mid);
	$data['inputtime'] = time();
	$data['updatetime'] = time();
	if($id = M('cms_category')->add($data)) {
		unset($data);
		$data['siteid'] = $id;
		$data['display_order'] = $id;
		M('cms_category')->where('id='.$id)->save($data);
		$data['siteid'] = $id;
		$data['name'] = '首页';
		$data['domain'] = SITE_PATH;
		$data['status'] = '1';
		M('cms_site')->add($data);
	}

    $sql_file = APPS_PATH.'/cms/Appinfo/function';
    if ($handle = opendir($sql_file)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $type = pathinfo($file, PATHINFO_EXTENSION);
                $filename = pathinfo($file, PATHINFO_FILENAME);
                if ($type === 'sql') {
                    $res = file_get_contents($sql_file.'/'.$file);
                    mysql_query('drop function if exists '.$filename.'; DELIMITER //');
                    mysql_query($res);
                }
            }
        }
        closedir($handle);
    }
}